<?php 
namespace backend\controllers;
use Yii;
use backend\controllers\CommontController;
use backend\models\right\JobRight;
use backend\models\admin\JobAdmin;
use backend\models\right\JobAdminRelation;
use yii\db\query;

class RightController extends CommontController
{	

    public function actionList()
    {   
    	$query = new Query();
        //$list = JobRight::find()->All();
        $list = $query->from('job_right')->All();
        $data = $this->NodeOrder($list,"right_pid");
        //print_r($data);exit;
        return $this->render('list',['list'=>$data]);      
    }
    public function actionAdd()
    {   
        $db = new JobRight();
        if (Yii::$app->request->isPost) 
        {   
            $data = Yii::$app->request->post();
            $name = $data['name'];
            $right = $data['right'];
            $right_list = JobRight::find()->where("name='$name' or `right`='$right'")->One();
            if ($right_list) {
               echo "<script>alert('权限名或者地址已存在！');
                location.href='?r=right/add'</script>";exit;
            }
            $db->name = $name;
            $db->right = $right;
            $db->is_del = 0;
            $db->right_pid = $data['right_pid'];
            $db->right_status = $data['right_status'];
            if ($db->insert()) 
            {
                return $this->redirect(['list']);
            }
        }else{

        $right = JobRight::find()->where(['right_pid'=>0])->all();

        return $this->render('add',['right'=>$right]);     
        }
             
    }
    public function actionAllot()
    {
        if (Yii::$app->request->isPost) 
        {   
            $data = Yii::$app->request->post();
            $res=Yii::$app->db->createCommand()->delete('job_admin_relation', ['admin_id' => $data['admin_id']])->execute();
            foreach ($data['right_id'] as $key => $value) 
            {
                $query = new Query();
                $arr['admin_id'] = $data['admin_id'];
                $arr['right_id'] = $value;
                Yii::$app->db->createCommand()->insert('job_admin_relation', $arr)->execute();
            }
               return $this->redirect(['admin/list']);
        }else
        {   
            $query = new Query(); 
            $id = Yii::$app->request->get('id');
            $admin = JobAdmin::find()->where(['id'=>$id])->One();
            $admin_id = $admin->id;
            $admin_right = JobAdminRelation::find()->where(['admin_id'=>$admin_id])->All();
            $admin_relation = [];
            if ($admin_right) 
            {
                foreach ($admin_right as $key => $value) 
                {      
                    $admin_relation[$key] = $query->from('job_right')->where(['id'=>$value->right_id])->One();
                }               
            }
            $relation_all = $query->from('job_right')->where("id>0")->All();
            $new_rel = $this->NodeOrder($relation_all,"right_pid");
            $data['relation_all'] = $new_rel;
            $data['admin_relation'] = $admin_relation;
            $data['admin']['admin_name'] = $admin->admin_name;
            $data['admin']['admin_id'] = $admin->id;
            return $this->render('allot',['data'=>$data]);
        }
    }


}


 ?>